package com.hevs.samplewebapp.server.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Abstract util class which provides md5 encoding method
 * 
 * @author Arnaud Durand
 * @date 27.04.2012
 */
public abstract class MD5Encoder {

	/**
	 * @param passphrase
	 * the string to be md5 encoded
	 * @return hashword
	 * @throws UnsupportedEncodingException
	 */
	public static String encode(String passphrase) throws UnsupportedEncodingException{
		String hashword = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(passphrase.getBytes("UTF-8"));
			BigInteger hash = new BigInteger(1, md5.digest());
			hashword = hash.toString(16);
		} catch (NoSuchAlgorithmException nsae) {
			//exception handling not needed because MD5 param is static and exists
			nsae.printStackTrace();
		}
		
		return hashword;
	}
}
